#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年9月18日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]


import sys,re,os
import inspect,datetime,time
import urllib,urllib2
import json
import copy
import logging

class ApiRequest():
    def __init__(self):
        pass

    def getResponse(self,url,detailMap={},data=None,type='post'):
        #return "{'code':1000,'data':[]}"
        if data==None:
#             print 'request URL:',url
#             print 'request paramap:',detailMap
            groupList = []
            listKeys = []
            for key,values in detailMap.iteritems():
                if isinstance(values,list):
                    listKeys.append(key)
                    
                    #del detailMap[key]
            for lk in listKeys:
                values = detailMap[lk]
                del detailMap[lk]
                count = 0
                for value in values:
                    count+=1
                    detailMap[lk+'_'+str(count)] = value
            
            data=urllib.urlencode(detailMap)
            for lk in listKeys:
                data = re.sub('%s_[\d]+'%lk,lk,data)
            
        else:
            data = data
        #data = 'RequestType=4&Token=10002000&TimeType=2&StartTime=2014-12-15&GroupType=4&OrderItem=15&OrderType=1&TotalPages=0&PageNumber=10&CurrentPage=1&GroupList=32010250&GroupList=32010249&GroupList=32010252&GroupList=32011163&GroupList=32011165&GroupList=32011167&GroupList=32011168&GroupList=32011169&GroupList=32011170&GroupList=32011172&GroupList=32011173&GroupList=32011174&GroupList=32011175&GroupList=32011176&GroupList=32011177&GroupList=32011178&GroupList=32011179&GroupList=32011180&GroupList=32011181&GroupList=32011182&GroupList=32011183&GroupList=32011184&GroupList=32011185&GroupList=32011186&GroupList=32011187&GroupList=32011188&GroupList=32011189&GroupList=32011190&GroupList=32011191&GroupList=32011192&GroupList=32011193&GroupList=32011194&GroupList=32011195&GroupList=32011196&GroupList=32011197&GroupList=32011198&GroupList=32011199&GroupList=32011200&GroupList=32011202&GroupList=32011203&GroupList=32011204&GroupList=32011205&GroupList=32011206&GroupList=32011207&GroupList=32011208&GroupList=32011283&GroupList=32011313&GroupList=32011353&GroupList=32011354&GroupList=32011355&GroupList=32011356&GroupList=32011357&GroupList=32011358&GroupList=32011359&GroupList=32011360&GroupList=32011361&GroupList=32011362&GroupList=32011363&GroupList=32011364&GroupList=32011365&GroupList=32011366&GroupList=32011367&GroupList=32011373&GroupList=32011374&GroupList=32011375&GroupList=32011376&GroupList=32011377&GroupList=32011378&GroupList=32011379&GroupList=32011380&GroupList=32011381&GroupList=32011382&GroupList=32011383&GroupList=32011384&GroupList=32011385&GroupList=32011386&GroupList=32011387&GroupList=32011388&GroupList=32011389&GroupList=32011390&GroupList=32011391&GroupList=32011392&GroupList=32011393&GroupList=32011394&GroupList=32011395&GroupList=32011396&GroupList=32011397&GroupList=32011967&GroupList=32011399&GroupList=32011400&GroupList=32011401&GroupList=32011402&GroupList=32011403&GroupList=32011404&GroupList=32011405&GroupList=32011406&GroupList=32011407&GroupList=32011408&GroupList=32011409&GroupList=32011410&GroupList=32011411&GroupList=32011412&GroupList=32011413&GroupList=32011414&GroupList=32011415&GroupList=32011416&GroupList=32011417&GroupList=32011418&GroupList=32011419&GroupList=32011420&GroupList=32011421&GroupList=32011422&GroupList=32011423&GroupList=32011424&GroupList=32011425&GroupList=32011426&GroupList=32011427&GroupList=32011428&GroupList=32011429&GroupList=32011430&GroupList=32011432&GroupList=32011433&GroupList=32011434&GroupList=32011435&GroupList=32011436&GroupList=32011437&GroupList=32011438&GroupList=32011439&GroupList=32011440&GroupList=32011441&GroupList=32011442&GroupList=32011444&GroupList=32011445&GroupList=32011446&GroupList=32011447&GroupList=32011448&GroupList=32011449&GroupList=32011450&GroupList=32011451&GroupList=32011452&GroupList=32011453&GroupList=32011454&GroupList=32011455&GroupList=32011456&GroupList=32011457&GroupList=32011458&GroupList=32011459&GroupList=32011460&GroupList=32011461&GroupList=32011462&GroupList=32011463&GroupList=32011464&GroupList=32011465&GroupList=32011466&GroupList=32011467&GroupList=32011530&GroupList=32011531&GroupList=32011532&GroupList=32011533&GroupList=32011534&GroupList=32011535&GroupList=32011536&GroupList=32011537&GroupList=32011538&GroupList=32011539&GroupList=32011540&GroupList=32011541&GroupList=32011542&GroupList=32011543&GroupList=32011544&GroupList=32011545&GroupList=32011546&GroupList=32011547&GroupList=32011548&GroupList=32011549&GroupList=32011550&GroupList=32011551&GroupList=32011552&GroupList=32011553&GroupList=32011554&GroupList=32011555&GroupList=32011556&GroupList=32011557&GroupList=32011558&GroupList=32011559&GroupList=32011560&GroupList=32011561&GroupList=32011562&GroupList=32011563&GroupList=32011564&GroupList=32011565&GroupList=32011566&GroupList=32011567&GroupList=32011568&GroupList=32011569&GroupList=32011570&GroupList=32011571&GroupList=32011572&GroupList=32011573&GroupList=32011574&GroupList=32011575&GroupList=32011576&GroupList=32011577&GroupList=32011578&GroupList=32011579&GroupList=32011580&GroupList=32011581&GroupList=32011582&GroupList=32011583&GroupList=32011584&GroupList=32011585&GroupList=32011586&GroupList=32011587&GroupList=32011588&GroupList=32011589&GroupList=32011590&GroupList=32011591&GroupList=32011592&GroupList=32011593&GroupList=32011594&GroupList=32011595&GroupList=32011596&GroupList=32011597&GroupList=32011598&GroupList=32011599&GroupList=32011966&GroupList=32011601&GroupList=32011602&GroupList=32011603&GroupList=32011605&GroupList=32011606&GroupList=32011607&GroupList=32011608&GroupList=32011609&GroupList=32011610&GroupList=32011611&GroupList=32011612&GroupList=32011965&GroupList=32011614&GroupList=32011615&GroupList=32011616&GroupList=32011617&GroupList=32011618&GroupList=32011619&GroupList=32011620&GroupList=32011621&GroupList=32011622&GroupList=32011623&GroupList=32011624&GroupList=32011625&GroupList=32011626&GroupList=32011627&GroupList=32011628&GroupList=32011629&GroupList=32011630&GroupList=32011631&GroupList=32011632&GroupList=32011968&GroupList=32011638&GroupList=32011639&GroupList=32011640&GroupList=32011641&GroupList=32011642&GroupList=32011643&GroupList=32011644&GroupList=32011645&GroupList=32011646&GroupList=32011647&GroupList=32011648&GroupList=32011649&GroupList=32011650&GroupList=32011651&GroupList=32011652&GroupList=32011653&GroupList=32011654&GroupList=32011655&GroupList=32011656&GroupList=32011657&GroupList=32011658&GroupList=32011659&GroupList=32011660&GroupList=32011661&GroupList=32011662&GroupList=32011663&GroupList=32011664&GroupList=32011665&GroupList=32011666&GroupList=32011667&GroupList=32011668&GroupList=32011669&GroupList=32011670&GroupList=32011671&GroupList=32011672&GroupList=32011673&GroupList=32011674&GroupList=32011675&GroupList=32011676&GroupList=32011677&GroupList=32011678&GroupList=32011679&GroupList=32011680&GroupList=32011681&GroupList=32011682&GroupList=32011683&GroupList=32011684&GroupList=32011685&GroupList=32011686&GroupList=32011687&GroupList=32011688&GroupList=32011689&GroupList=32011690&GroupList=32011691&GroupList=32011692&GroupList=32011693&GroupList=32011694&GroupList=32011695&GroupList=32011696&GroupList=32011697&GroupList=32011698&GroupList=32011699&GroupList=32011700&GroupList=32011701&GroupList=32011702&GroupList=32011703&GroupList=32011704&GroupList=32011705&GroupList=32011706&GroupList=32011707&GroupList=32011708&GroupList=32011709&GroupList=32011710&GroupList=32011711&GroupList=32011713&GroupList=32011714&GroupList=32011715&GroupList=32011716&GroupList=32011717&GroupList=32011718&GroupList=32011719&GroupList=32011720&GroupList=32011721&GroupList=32011722&GroupList=32011723&GroupList=32011724&GroupList=32011725&GroupList=32011726&GroupList=32011727&GroupList=32011728&GroupList=32011729&GroupList=32011730&GroupList=32011731&GroupList=32011732&GroupList=32011733&GroupList=32011734&GroupList=32011735&GroupList=32011736&GroupList=32011737&GroupList=32011738&GroupList=32011739&GroupList=32011740&GroupList=32011741&GroupList=32011742&GroupList=32011743&GroupList=32011744&GroupList=32011745&GroupList=32011746'
        #data = 'RequestType=4&Token=10002000&TimeType=2&StartTime=2014-12-15&GroupType=4&OrderItem=3&OrderType=1&TotalPages=0&PageNumber=10&CurrentPage=1&GroupList=32010250&GroupList=32010249&GroupList=32010252&GroupList=32011163&GroupList=32011165&GroupList=32011167&GroupList=32011168&GroupList=32011169&GroupList=32011170&GroupList=32011172&GroupList=32011173&GroupList=32011174&GroupList=32011175&GroupList=32011176&GroupList=32011177&GroupList=32011178&GroupList=32011179&GroupList=32011180&GroupList=32011181&GroupList=32011182&GroupList=32011183&GroupList=32011184&GroupList=32011185&GroupList=32011186&GroupList=32011187&GroupList=32011188&GroupList=32011189&GroupList=32011190&GroupList=32011191&GroupList=32011192&GroupList=32011193&GroupList=32011194&GroupList=32011195&GroupList=32011196&GroupList=32011197&GroupList=32011198&GroupList=32011199&GroupList=32011200&GroupList=32011202&GroupList=32011203&GroupList=32011204&GroupList=32011205&GroupList=32011206&GroupList=32011207&GroupList=32011208&GroupList=32011283&GroupList=32011313&GroupList=32011353&GroupList=32011354&GroupList=32011355&GroupList=32011356&GroupList=32011357&GroupList=32011358&GroupList=32011359&GroupList=32011360&GroupList=32011361&GroupList=32011362&GroupList=32011363&GroupList=32011364&GroupList=32011365&GroupList=32011366&GroupList=32011367&GroupList=32011373&GroupList=32011374&GroupList=32011375&GroupList=32011376&GroupList=32011377&GroupList=32011378&GroupList=32011379&GroupList=32011380&GroupList=32011381&GroupList=32011382&GroupList=32011383&GroupList=32011384&GroupList=32011385&GroupList=32011386&GroupList=32011387&GroupList=32011388&GroupList=32011389&GroupList=32011390&GroupList=32011391&GroupList=32011392&GroupList=32011393&GroupList=32011394&GroupList=32011395&GroupList=32011396&GroupList=32011397&GroupList=32011967&GroupList=32011399&GroupList=32011400&GroupList=32011401&GroupList=32011402&GroupList=32011403&GroupList=32011404&GroupList=32011405&GroupList=32011406&GroupList=32011407&GroupList=32011408&GroupList=32011409&GroupList=32011410&GroupList=32011411&GroupList=32011412&GroupList=32011413&GroupList=32011414&GroupList=32011415&GroupList=32011416&GroupList=32011417&GroupList=32011418&GroupList=32011419&GroupList=32011420&GroupList=32011421&GroupList=32011422&GroupList=32011423&GroupList=32011424&GroupList=32011425&GroupList=32011426&GroupList=32011427&GroupList=32011428&GroupList=32011429&GroupList=32011430&GroupList=32011432&GroupList=32011433&GroupList=32011434&GroupList=32011435&GroupList=32011436&GroupList=32011437&GroupList=32011438&GroupList=32011439&GroupList=32011440&GroupList=32011441&GroupList=32011442&GroupList=32011444&GroupList=32011445&GroupList=32011446&GroupList=32011447&GroupList=32011448&GroupList=32011449&GroupList=32011450&GroupList=32011451&GroupList=32011452&GroupList=32011453&GroupList=32011454&GroupList=32011455&GroupList=32011456&GroupList=32011457&GroupList=32011458&GroupList=32011459&GroupList=32011460&GroupList=32011461&GroupList=32011462&GroupList=32011463&GroupList=32011464&GroupList=32011465&GroupList=32011466&GroupList=32011467&GroupList=32011530&GroupList=32011531&GroupList=32011532&GroupList=32011533&GroupList=32011534&GroupList=32011535&GroupList=32011536&GroupList=32011537&GroupList=32011538&GroupList=32011539&GroupList=32011540&GroupList=32011541&GroupList=32011542&GroupList=32011543&GroupList=32011544&GroupList=32011545&GroupList=32011546&GroupList=32011547&GroupList=32011548&GroupList=32011549&GroupList=32011550&GroupList=32011551&GroupList=32011552&GroupList=32011553&GroupList=32011554&GroupList=32011555&GroupList=32011556&GroupList=32011557&GroupList=32011558&GroupList=32011559&GroupList=32011560&GroupList=32011561&GroupList=32011562&GroupList=32011563&GroupList=32011564&GroupList=32011565&GroupList=32011566&GroupList=32011567&GroupList=32011568&GroupList=32011569&GroupList=32011570&GroupList=32011571&GroupList=32011572&GroupList=32011573&GroupList=32011574&GroupList=32011575&GroupList=32011576&GroupList=32011577&GroupList=32011578&GroupList=32011579&GroupList=32011580&GroupList=32011581&GroupList=32011582&GroupList=32011583&GroupList=32011584&GroupList=32011585&GroupList=32011586&GroupList=32011587&GroupList=32011588&GroupList=32011589&GroupList=32011590&GroupList=32011591&GroupList=32011592&GroupList=32011593&GroupList=32011594&GroupList=32011595&GroupList=32011596&GroupList=32011597&GroupList=32011598&GroupList=32011599&GroupList=32011966&GroupList=32011601&GroupList=32011602&GroupList=32011603&GroupList=32011605&GroupList=32011606&GroupList=32011607&GroupList=32011608&GroupList=32011609&GroupList=32011610&GroupList=32011611&GroupList=32011612&GroupList=32011965&GroupList=32011614&GroupList=32011615&GroupList=32011616&GroupList=32011617&GroupList=32011618&GroupList=32011619&GroupList=32011620&GroupList=32011621&GroupList=32011622&GroupList=32011623&GroupList=32011624&GroupList=32011625&GroupList=32011626&GroupList=32011627&GroupList=32011628&GroupList=32011629&GroupList=32011630&GroupList=32011631&GroupList=32011632&GroupList=32011968&GroupList=32011638&GroupList=32011639&GroupList=32011640&GroupList=32011641&GroupList=32011642&GroupList=32011643&GroupList=32011644&GroupList=32011645&GroupList=32011646&GroupList=32011647&GroupList=32011648&GroupList=32011649&GroupList=32011650&GroupList=32011651&GroupList=32011652&GroupList=32011653&GroupList=32011654&GroupList=32011655&GroupList=32011656&GroupList=32011657&GroupList=32011658&GroupList=32011659&GroupList=32011660&GroupList=32011661&GroupList=32011662&GroupList=32011663&GroupList=32011664&GroupList=32011665&GroupList=32011666&GroupList=32011667&GroupList=32011668&GroupList=32011669&GroupList=32011670&GroupList=32011671&GroupList=32011672&GroupList=32011673&GroupList=32011674&GroupList=32011675&GroupList=32011676&GroupList=32011677&GroupList=32011678&GroupList=32011679&GroupList=32011680&GroupList=32011681&GroupList=32011682&GroupList=32011683&GroupList=32011684&GroupList=32011685&GroupList=32011686&GroupList=32011687&GroupList=32011688&GroupList=32011689&GroupList=32011690&GroupList=32011691&GroupList=32011692&GroupList=32011693&GroupList=32011694&GroupList=32011695&GroupList=32011696&GroupList=32011697&GroupList=32011698&GroupList=32011699&GroupList=32011700&GroupList=32011701&GroupList=32011702&GroupList=32011703&GroupList=32011704&GroupList=32011705&GroupList=32011706&GroupList=32011707&GroupList=32011708&GroupList=32011709&GroupList=32011710&GroupList=32011711&GroupList=32011713&GroupList=32011714&GroupList=32011715&GroupList=32011716&GroupList=32011717&GroupList=32011718&GroupList=32011719&GroupList=32011720&GroupList=32011721&GroupList=32011722&GroupList=32011723&GroupList=32011724&GroupList=32011725&GroupList=32011726&GroupList=32011727&GroupList=32011728&GroupList=32011729&GroupList=32011730&GroupList=32011731&GroupList=32011732&GroupList=32011733&GroupList=32011734&GroupList=32011735&GroupList=32011736&GroupList=32011737&GroupList=32011738&GroupList=32011739&GroupList=32011740&GroupList=32011741&GroupList=32011742&GroupList=32011743&GroupList=32011744&GroupList=32011745&GroupList=32011746'
        #data= 'Key=fceef6fbee394e70d651a89105f9a1f2&Timestamp=1439786608518&Token=6ce6b8aefe7836daaccfce72cd8f5ff4&GroupList=32015789&GroupType=4&StartTime=2015-08-01&RequestType=4&TimeType=1'
        st = time.time()
        if type=='post':
            #data = 'Token=10002000&StartTime=2014-05-20&EndTime=2014-06-10&GroupType=4&Dimension=2&CustomerType=0&Group=32010226'
            #data = 'Group=39&Key=gaodadezhimatechwoaizhimamofi200&Token=1a6680b05c4bf4b2abbeeec2f91da188&Timestamp=1433475370'
            logging.info( '%s?%s'%(url,data))
            req=urllib2.Request(url,data)
#             print req
            response=urllib2.urlopen(req)
        else:
            logging.info( '%s?%s'%(url,data))
            response = urllib2.urlopen(url+'?'+data)
            
        page=response.read()
        et = time.time()
        logging.info( 'use time:%s'%(et-st))
        
        fr = open('use_time.txt','a+')
        fr.write('%s\t%s\t%s\n'%(url,et-st,data))
        fr.close()
        return page
myApiRequest = ApiRequest()